package photoEffects
{
	import com.gskinner.utils.Rndm;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	
	import org.casalib.events.ProcessEvent;
	
	public class GhostEffect extends PhotoEffect
	{
		public function GhostEffect()
		{
			super();
		}
		
		public static const NAME:String = "Ghost Effect";
		
		/**
		 * Apply the effect on a Bitmap. When the apply is finished, a ProcessEvent.COMPLETE is dispatched.
		 * @param bitmap The source Bitmap.
		 * @param callBack A function that accept a Bitmap object. Called when apply is finished.
		 */
		public override function applyOn(bitmap:Bitmap, callBack:Function = null):void
		{
			var ran:Rndm = new Rndm(bitmap.bitmapData.getPixel(100,100) + bitmap.bitmapData.getPixel(200,200) - bitmap.bitmapData.getPixel(300,300));
			
			_workingBitmap = new Bitmap(bitmap.bitmapData.clone());
			
			for (var countNumOfGhost:uint = 0 ; countNumOfGhost < ran.integer(1,7); countNumOfGhost++){
				var imgData:BitmapData = new images[ran.integer(0,images.length-1)]().bitmapData;
				var tran:Matrix = new Matrix();
				var scale:Number = ran.float(0.1,0.3);
				tran.createBox(scale,scale,ran.float(0,360),ran.float(imgData.width*scale,bitmap.width-imgData.width*scale),ran.float(imgData.height*scale,bitmap.height-imgData.height*scale));
				_workingBitmap.bitmapData.draw(imgData,tran);
				imgData.dispose();
			}
			
			
			_resultBitmap = _workingBitmap;
			_workingBitmap = null;
			
			dispatchEvent(new ProcessEvent(ProcessEvent.COMPLETE));
			
			if(callBack != null){
				callBack(_resultBitmap);
			}
		}
		
		[Embed(source="../../graphics/effectObject/ghost/g1.png")]
		private static var g1:Class;
		
		[Embed(source="../../graphics/effectObject/ghost/g2.png")]
		private static var g2:Class;
		
		[Embed(source="../../graphics/effectObject/ghost/g3.png")]
		private static var g3:Class;
		
		[Embed(source="../../graphics/effectObject/ghost/g4.png")]
		private static var g4:Class;
		
		[Embed(source="../../graphics/effectObject/ghost/g5.png")]
		private static var g5:Class;
		
		[Embed(source="../../graphics/effectObject/ghost/m1.png")]
		private static var m1:Class;
		
		[Embed(source="../../graphics/effectObject/ghost/m2.png")]
		private static var m2:Class;
		
		[Embed(source="../../graphics/effectObject/ghost/m3.png")]
		private static var m3:Class;
		
		[Embed(source="../../graphics/effectObject/ghost/mao.png")]
		private static var mao:Class;
		
		private static var images:Array = [g1,g2,g3,g4,g5,m1,m2,m3,mao];
	}
}